//
//  PlSInputManagerPlugIn.m
//  Injector
//
//  Created by ∞ on 19/11/07.
//  Copyright 2007 Emanuele Vulcano. All rights reserved.
//

#import "PlSInputManagerPlugIn.h"
#import "PlSAppSpecifier.h"
#import "PlSInputManagerManager.h"

@implementation PlSInputManagerPlugIn

@synthesize identifier, version, appSpecifiers;

- (BOOL) isLoadable {
	return [manager isLoadable:self];
}

- (void) setLoadable:(BOOL) l {
	[manager setLoadable:l forPlugIn:self];
}

- (id) initWithPath:(NSString*) item manager:(PlSInputManagerManager*) m isLocal:(BOOL) local {
#define PlSReturnNilUnless(x) if (!(x)) { [self release]; return nil; }
	if (!(self = [super init]))
		return nil;
	isLocal = local;
	manager = m;	

	NSDictionary* info2 = [NSDictionary dictionaryWithContentsOfFile:[item stringByAppendingPathComponent:@"Info"]];
	PlSReturnNilUnless(info2);
		
	PlSReturnNilUnless([[info2 objectForKey:@"LoadBundleOnLaunch"] boolValue] &&
					   [[info2 objectForKey:@"NoMenuEntry"] boolValue]);
	
	NSString* bundleName = [info2 objectForKey:@"BundleName"];
	PlSReturnNilUnless(bundleName && [bundleName isKindOfClass:[NSString class]]);
	
	NSBundle* bundle = [NSBundle bundleWithPath:[item stringByAppendingPathComponent:bundleName]];
	PlSReturnNilUnless(bundle);
	
	NSDictionary* info = [bundle infoDictionary];
	PlSReturnNilUnless(info);
	PlSReturnNilUnless(identifier = [[info objectForKey:@"CFBundleIdentifier"] retain]);
	
	id versionObj = [info objectForKey:@"CFBundleVersion"];
	version = versionObj? [versionObj doubleValue] : 0;
	
	appSpecifiers = [[NSArray arrayWithObject:[PlSAppSpecifier anyApplication]] retain];
	plugInBundle = [bundle retain];
	plugInPath = [item retain];
	
	return self;
#undef PlSReturnNilUnless
}

- (void) dealloc {
	[plugInBundle release];
	[plugInPath release];
	[identifier release];
	[appSpecifiers release];
	
	[super dealloc];
}

- (BOOL) loadNow {
	Class c = [plugInBundle principalClass];
	if (!c) return NO;
	if ([[c alloc] init]) return YES;
	else return NO;
}

- (NSString*) displayName {
	NSString* dn = [plugInBundle objectForInfoDictionaryKey:@"CFBundleName"];
	if (!dn)
		dn = [[[NSFileManager defaultManager] displayNameAtPath:[plugInBundle bundlePath]] stringByDeletingPathExtension];
	
	return dn;
}

- (BOOL) isManaged { return !isLocal; }


- (NSImage*) icon {
	NSImage* icon = [[NSWorkspace sharedWorkspace] iconForFile:[plugInBundle bundlePath]];
	[icon setSize:NSMakeSize(32, 32)];
	return icon;
}

- (NSString*) userVisiblePath {
	return [[plugInBundle bundlePath] stringByDeletingLastPathComponent];
}

- (NSURL*) webSiteURL { return nil; }
- (NSURL*) updatesCheckURL { return nil; }
- (BOOL) canRemoveByDeletingUserVisiblePath { return YES; }
- (BOOL) canCheckQuarantineOnUserVisiblePath { return YES; }

- (double) currentVersion {
	id v = [plugInBundle objectForInfoDictionaryKey:@"CFBundleVersion"];
	return v? [v doubleValue] : 0.0;
}

- (NSString*) currentShortVersionString {
	return [plugInBundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}

@end
